package com.bm.web.servlet;

import java.io.IOException;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.bm.entity.Img;
import com.bm.service.ImgService;
import com.bm.service.impl.ImgServiceImpl;

/**
 * Servlet implementation class InsertImgServlet
 */
@WebServlet("/InsertImgServlet")
public class InsertImgServlet extends HttpServlet {
	private static final long serialVersionUID = 1L;
       
    /**
     * @see HttpServlet#HttpServlet()
     */
    public InsertImgServlet() {
        super();
        // TODO Auto-generated constructor stub
    }

	/**
	 * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		// TODO Auto-generated method stub
		request.setCharacterEncoding("UTF-8");
		String urls = request.getParameter("urls");
		String types = request.getParameter("types");
		String xiln = request.getParameter("xiln");
		Img img = new Img();
		ImgService is = new ImgServiceImpl();
		
		if(urls.equals("")) {
			request.setAttribute("cs", "添加失败，请写路径");
			request.getRequestDispatcher("insert.jsp").forward(request, response);
		}
		
		if(types.equals("")) {
			request.setAttribute("cs", "添加失败，请写系列");
			request.getRequestDispatcher("insert.jsp").forward(request, response);
		}else if(types.equals("白底黑石子系列")) {
			img.setBsName("blackone");
		}else if(types.equals("白色系列")) {
			img.setBsName("white");
		}else if(types.equals("爆款灰色系列")) {
			img.setBsName("grey");
		}else if(types.equals("贝壳系列")) {
			img.setBsName("seashell");
		}else if(types.equals("玻璃石系列")) {
			img.setBsName("glass");
		}else if(types.equals("丹东绿系列")) {
			img.setBsName("Dandong");
		}else if(types.equals("粉色系")) {
			img.setBsName("pink");
		}else if(types.equals("黑色系列")) {
			img.setBsName("blacktwo");
		}else if(types.equals("红色系")) {
			img.setBsName("red");
		}else if(types.equals("灰色彩石系列")) {
			img.setBsName("color");
		}else if(types.equals("灰色黑石子系列")) {
			img.setBsName("greeblack");
		}else if(types.equals("灰色细砂系列")) {
			img.setBsName("sand");
		}else if(types.equals("蓝色系")) {
			img.setBsName("blue");
		}else if(types.equals("绿色系")) {
			img.setBsName("green");
		}else if(types.equals("浅黄色系")) {
			img.setBsName("yellow");
		}else if(types.equals("浅灰石子系列")) {
			img.setBsName("lightgray");
		}else if(types.equals("奢石系列")) {
			img.setBsName("extravagant");
		}else if(types.equals("五彩石系列")) {
			img.setBsName("multicolored");
		}else if(types.equals("中灰系列")) {
			img.setBsName("mediumgray");
		}else {
			request.setAttribute("cs", "添加失败，系列名错误");
			request.getRequestDispatcher("insert.jsp").forward(request, response);
		}
		
		if(xiln.equals("")) {
			request.setAttribute("cs", "添加失败，请写类型");
			request.getRequestDispatcher("insert.jsp").forward(request, response);
		}
		img.setImgurl(urls);
		img.setType(types);
		img.setTitle(xiln);
		boolean tf = is.insert(img);
		if(tf == true) {
			request.setAttribute("cs", "添加成功");
			request.getRequestDispatcher("insert.jsp").forward(request, response);
		}else {
			request.setAttribute("cs", "添加失败");
			request.getRequestDispatcher("insert.jsp").forward(request, response);
		}
	}

	/**
	 * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		// TODO Auto-generated method stub
		doGet(request, response);
	}

}
